package com.xierong.remoteplayerlauncher.recevier;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.xierong.remoteplayerlauncher.utils.ExteralStorageUtils;

public class ExternalStorageReceiver extends BroadcastReceiver {
    public static final String EXTERNAL_STORAGE_CONFIG_PATH= "com.xierong.external_storage_config_path";
    public static final String EXTERNAL_STORAGE_UNMOUNTED = "com.xierong.external_stroage_unmounted";
    public static final String EXTERNAL_STORAGE_NO_CONFIG = "com.xierong.external_storage_noconfig";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action  = intent.getAction();
        String path = intent.getData().getPath();
        if(!TextUtils.isEmpty(path)) {
            if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
                Intent unmountedIntent = new Intent(EXTERNAL_STORAGE_UNMOUNTED);
                context.sendBroadcast(unmountedIntent);
            }
            if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
                String configFilePath = ExteralStorageUtils.getInstance().getExternalConfigFile(context);
                if (configFilePath != null) {
                    Intent intent1 = new Intent(EXTERNAL_STORAGE_CONFIG_PATH);
                    intent1.putExtra("path", configFilePath);
                    context.sendBroadcast(intent1);
                } else {
                    Intent intent1 = new Intent(EXTERNAL_STORAGE_NO_CONFIG);
                    context.sendBroadcast(intent1);
                }
            }
            if (Intent.ACTION_MEDIA_BAD_REMOVAL.equals(action)) {
                Intent unmountedIntent = new Intent(EXTERNAL_STORAGE_UNMOUNTED);
                context.sendBroadcast(unmountedIntent);
            }
        }
    }
}
